package cn.fl.spl.service.impl;

import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.logger.eum.LogExceptionEnum;
import cn.cmm.base.framework.logger.eum.LogOperEnum;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.spl.service.ISplOpLogRecService;
import cn.fl.spl.transform.SplOpLogRecVOTransform;
import cn.fl.spl.vo.SplOpLogRecVO;
import fl.constant.facade.framework.constant.BusinessIdConstant;
import fl.mongo.facade.base.page.PaginationVo;
import fl.mongo.facade.spl.api.ISplOpLogRecApiService;
import fl.mongo.facade.spl.api.ISplOpLogRecQueryApiService;
import fl.mongo.facade.spl.condition.SplOpLogRecQC;
import fl.mongo.facade.spl.dto.SplOpLogRecDTO;
import fl.spl.facade.framework.constant.SplSystemConstant;
import fl.spl.facade.framework.enums.SplRelLogBizTypCdNum;
import fl.spl.facade.lnkcomp.api.IPrjLnkCompQueryApiService;
import fl.spl.facade.lnkcomp.dto.PrjLnkCompDTO;
import fl.spl.facade.mfr.api.IMfrInfoApiService;
import fl.spl.facade.mfr.dto.MfrInfoDTO;
import fl.spl.facade.spl.api.ISplInfoApiService;
import fl.spl.facade.spl.dto.SplInfoDTO;
import fl.spl.facade.store.api.ISplRelOpLogApiService;
import fl.spl.facade.store.dto.SplRelOpLogDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Description: 供应商操作日志记录 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get*
 *               批量：batch* 查询集合：search*
 */
@Service
public class SplOpLogServiceImpl implements  ISplOpLogRecService{


	@Autowired
	private ISplOpLogRecApiService splOpLogRecApiService;

	@Autowired
	private ISplInfoApiService splInfoApiService;

	@Autowired
	private IPrjLnkCompQueryApiService prjLnkCompQueryApiService;

	@Autowired
	private ISplOpLogRecQueryApiService splOpLogRecQueryApiService;

	@Autowired
	private ISplRelOpLogApiService splRelOpLogApiService;

	@Autowired
	private IMfrInfoApiService mfrInfoApiService;

	@Override
	public SplOpLogRecVO selectById(Long id) throws BaseException {
		if (StringUtils.isEmpty(id)) {
			throw new BaseException(BaseExceptionConstant.COMMON_0001, "获取对象主键不能为空!");
		}
		// TODO 业务逻辑
		SplOpLogRecDTO dto = null;// this.自定义ApiService.selectSplVisitRecDTOById(id);
		return SplOpLogRecVOTransform.toVO(dto);
	}

	@Override
	public Long add(String dataId, String reason, String opTypCd, String opObjTyp) throws BaseException {
		if (StringUtils.isEmpty(dataId) && StringUtils.isEmpty(reason)) {
			BaseLogUtils.newWebLogger("主键参数为空").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.warn(LogExceptionEnum.COMMON_CODE_0002);
			throw new BaseException(BaseExceptionConstant.COMMON_0001, "参数不完整!");
		}
		
		SplInfoDTO splInfoDTO = null;
		PrjLnkCompDTO lnkCompDTO = null;
		MfrInfoDTO mfrInfoDTO = null;
		if (SplRelLogBizTypCdNum.SPL.getBizTypCd().equals(opObjTyp)) {
			splInfoDTO = this.splInfoApiService.selectSplInfoById(Long.valueOf(dataId));
		} else if(SplRelLogBizTypCdNum.LNK.getBizTypCd().equals(opObjTyp)){
			lnkCompDTO = this.prjLnkCompQueryApiService.selectPrjLnkCompById(Long.valueOf(dataId));
		}else if(SplRelLogBizTypCdNum.MFR.getBizTypCd().equals(opObjTyp)){
			mfrInfoDTO = this.mfrInfoApiService.selectPrdMfrInfoById(Long.valueOf(dataId), SplSystemConstant.FL_SYS_CD,SplSystemConstant.DEFAULT_ORG_CD);
		}
		
		SplRelOpLogDTO dto = new SplRelOpLogDTO();
		dto.setDataId(Long.valueOf(dataId));
		if (SplRelLogBizTypCdNum.SPL.getBizTypCd().equals(opObjTyp)) {
			dto.setDataNm(splInfoDTO.getSplNm());
		} else if(SplRelLogBizTypCdNum.LNK.getBizTypCd().equals(opObjTyp)){
			dto.setDataNm(lnkCompDTO.getLnkCompNm());
		}else if(SplRelLogBizTypCdNum.MFR.getBizTypCd().equals(opObjTyp)){
			dto.setDataNm(mfrInfoDTO.getMfrNm());
		}
		dto.setOpDesc(reason);
		dto.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
		dto.setCrtUsrName(CurrentThreadContext.getCurrentUserName());
		dto.setIsDel(BaseConstant.IS_YESNO_NO);
		dto.setIsNew(BaseConstant.IS_YESNO_YES);
		dto.setIsValid(BaseConstant.IS_YESNO_YES);
		dto.setOpTypCd(opTypCd);
		dto.setCrtTm(new Date());
		dto.setBizTypCd(opObjTyp);
		dto.setOpSource(SplSystemConstant.FL_WEB_CHL_CD);
		this.splRelOpLogApiService.addSplRelOpLogDTO(dto);
		return null;
	}

	@Override
	public boolean removeById(Long id) throws BaseException {
		if (StringUtils.isEmpty(id)) {
			throw new BaseException(BaseExceptionConstant.COMMON_0001, "删除对象主键不能为空!");
		}
		;
		// return this.splVisitRecApiService.removeSplVisitRecDTOById(id);
		// TODO 业务逻辑
		return false;
	}

	/**
	 * 删除
	 * 
	 * @param ids
	 * @return
	 */
	@Override
	public boolean removeByIds(List<Long> ids) throws BaseException {
		if (CollectionUtils.isEmpty(ids)) {
			throw new BaseException(BaseExceptionConstant.COMMON_0001, "删除对象主键不能为空!");
		}
		;
		return false;
	}

	@Override
	public boolean modify(SplOpLogRecVO entity) throws BaseException {
		if (null == entity) {
			throw new BaseException(BaseExceptionConstant.COMMON_0001, "修改对象不能为空!");
		}
		;
		// return
		// this.splVisitRecApiService.modifySplVisitRecDTO(SplVisitRecVOTransform.toDTO(entity));
		// TODO 业务逻辑
		return false;
	}

	@Override
	public List<SplOpLogRecVO> searchSplVisitRecList(SplOpLogRecQC condition) throws BaseException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PaginationVo searchListPage(PaginationVo paginationVo) throws BaseException {
		if (null == paginationVo) {
			return null;
		}
		PaginationVo resultPage = this.splOpLogRecQueryApiService.findSplOpLogRecDTOByPage(paginationVo);
		return resultPage;
	}
}
